package com.hyt.it.ogt.controller.pj;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.controller.base.BaseController;
import com.hyt.it.ogt.pj.common.Constants;
import com.hyt.it.ogt.pj.common.ResponseCode;
import com.hyt.it.ogt.pj.model.entity.Subject;
import com.hyt.it.ogt.pj.service.ISubjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;

/**
 * <p>
 * 评卷科目表 前端控制器
 * </p>
 *
 * @author qiaozj
 * @since 2020-07-09
 */
@ApiSort(value = 2)
@Api(tags = "2.科目接口", value = "科目接口")
@RestController
@RequestMapping("/subject")
public class SubjectController extends BaseController {

    @Autowired
    private ISubjectService subjectService;

    @ApiOperationSort(value = 1)
    @ApiOperation(value = "2.1.1根据id获取科目", notes = Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("query/{id}")
    public Object query(@PathVariable("id") String id){
        Subject subject = subjectService.getById(id);
        if(subject != null){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).data(subject).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 10)
    @ApiOperation(value = "2.2.1添加科目", notes= Constants.SWAGGER_METHOD_NOTES)
    @GetMapping("add")
    public Object add(@RequestBody Subject subject){
        Boolean flag = subjectService.save(subject);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 20)
    @ApiOperation(value = "2.3.1更新科目", notes = Constants.SWAGGER_METHOD_NOTES)
    @PutMapping("update")
    public Object update(@RequestBody Subject subject){
        boolean flag = subjectService.updateById(subject);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

    @ApiOperationSort(value = 30)
    @ApiOperation(value = "2.4.1根据id删除科目", notes = Constants.SWAGGER_METHOD_NOTES)
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") String id){
        boolean flag = subjectService.removeById(id);
        if(flag){
            return ApiResponse.builder().code(ResponseCode.SUCCESS).build();
        }else{
            return ApiResponse.builder().code(ResponseCode.ERROR).build();
        }
    }

}
